class ListNode {
    val: number;
    next: ListNode | null;
    constructor(val?: number, next?: ListNode | null) {
        this.val = val === undefined ? 0 : val;
        this.next = next === undefined ? null : next;
    }
}

function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
    // 创建哑节点作为新链表的起始点
    const dummy = new ListNode(-1);
    let current = dummy;

    // 当两个链表都不为空时，比较节点值
    while (list1 && list2) {
        if (list1.val <= list2.val) {
            current.next = list1;
            list1 = list1.next;
        } else {
            current.next = list2;
            list2 = list2.next;
        }
        current = current.next;
    }
    
    // 将剩余的链表直接接上
    current.next = list1 ?? list2;

    return dummy.next;
};